package com.wgchao.pharmacymg.domain.dao;

import com.wgchao.pharmacymg.Infrastructure.exception.define.ObjectNotExistedException;
import com.wgchao.pharmacymg.Infrastructure.repository.PhDrugInfoRepository;
import com.wgchao.pharmacymg.domain.entity.PhDrugInfoDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: PhDrugInfoDao
 * @author: wgchao
 * @createTime: 2019/11/18 10:15 AM
 * @Description: TODO
 */
@Service
public class PhDrugInfoDao {
    @Autowired
    private PhDrugInfoRepository drugInfoRepository;

    public PhDrugInfoDO save(PhDrugInfoDO drugInfoDO){
        drugInfoDO = drugInfoRepository.save(drugInfoDO);
        return drugInfoDO;
    }

    public PhDrugInfoDO findById(Long drugId){
        Optional<PhDrugInfoDO> drugInfoDOOptional = drugInfoRepository.findById(drugId);
        if(!drugInfoDOOptional.isPresent()){
            throw new ObjectNotExistedException("药品不存在");
        }
        return drugInfoDOOptional.get();
    }

    public Page<PhDrugInfoDO> page(Specification specification, Pageable pageable){
        return drugInfoRepository.findAll(specification, pageable);
    }

    public List<PhDrugInfoDO> qbc(Specification specification, Sort sort){
        return drugInfoRepository.findAll(specification, sort);
    }
}
